package chapter4.No4_1_使用reentrantLock类.demo18;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * *：
 * 使用Condition实现顺序执行
 * Created by Administrator on 2016/11/22.
 */
public class Main {

    private static ReentrantLock lock = new ReentrantLock();
    final static Condition aCondition = lock.newCondition();
    final static Condition bCondition = lock.newCondition();
    final static Condition cCondition = lock.newCondition();
    private volatile static int nextWho = 1;

    public static void main(String[] args) {

        Runnable aRunnable = new Runnable() {
            public void run() {
                try {
                    lock.lock();
                    while (nextWho != 1) {
                        aCondition.await();
                    }
                    for (int i = 0; i < 3; i++) {
                        System.out.println(Thread.currentThread().getName() + " " + (i+1));
                    }
                    nextWho = 2;
                    bCondition.signalAll();
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable bRunnable = new Runnable() {
            public void run() {
                try {
                    lock.lock();
                    while (nextWho != 2) {
                        aCondition.await();
                    }
                    for (int i = 0; i < 3; i++) {
                        System.out.println(Thread.currentThread().getName() + " " + (i+1));
                    }
                    nextWho = 3;
                    cCondition.signalAll();
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable cRunnable = new Runnable() {
            public void run() {
                try {
                    lock.lock();
                    while (nextWho != 3) {
                        aCondition.await();
                    }
                    for (int i = 0; i < 3; i++) {
                        System.out.println(Thread.currentThread().getName() + " " + (i+1));
                    }
                    nextWho = 1;
                    aCondition.signalAll();
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread[] aThreads = new Thread[5];
        Thread[] bThreads = new Thread[5];
        Thread[] cThreads = new Thread[5];
        for(int i = 0; i < 5; i++) {
            aThreads[i] = new Thread(aRunnable);
            aThreads[i].setName("a"+i);
            bThreads[i] = new Thread(bRunnable);
            bThreads[i].setName("b"+i);
            cThreads[i] = new Thread(cRunnable);
            cThreads[i].setName("c"+i);
        }

        for(int i = 0; i < 5; i++) {
            aThreads[i].start();
            bThreads[i].start();
            cThreads[i].start();
        }

    }

}
